﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Data;
using System.Xml.Linq;
using System.IO;

namespace Qpm.Automap.Core
{
  public static class ProjectReader
  {
    public static DataTable GetProjectTasks(string fileName)
    {
      XmlDocument xdoc = new XmlDocument();
      xdoc.Load(fileName);
      XmlNamespaceManager xnm = new XmlNamespaceManager(xdoc.NameTable);
      xnm.AddNamespace("p", "http://schemas.microsoft.com/project");
      DataTable taskTable = new DataTable();
      taskTable.Columns.Add("Select", typeof(bool));
      taskTable.Columns.Add("ID", typeof(int));
      taskTable.Columns.Add("WBS", typeof(string));
      taskTable.Columns.Add("Name", typeof(string));
      taskTable.Columns.Add("UID", typeof(int));
      taskTable.Columns.Add("OutlineLevel", typeof(int));
      taskTable.Columns.Add("OutlineNumber", typeof(string));
      taskTable.Columns.Add("IsSummary", typeof(bool));
      taskTable.Columns.Add("Predecessors", typeof(string));

      foreach (XmlElement taskNode in xdoc.SelectNodes("//p:Tasks/p:Task", xnm))
      {
        DataRow row = taskTable.NewRow();
        XmlNode uidNode = taskNode.SelectSingleNode("p:UID", xnm);
        XmlNode idNode = taskNode.SelectSingleNode("p:ID", xnm);
        XmlNode wbsNode = taskNode.SelectSingleNode("p:WBS", xnm);
        XmlNode nameNode = taskNode.SelectSingleNode("p:Name", xnm);
        XmlNode outlineLevelNode = taskNode.SelectSingleNode("p:OutlineLevel", xnm);
        XmlNode outlineNumberNode = taskNode.SelectSingleNode("p:OutlineNumber", xnm);
        XmlNode summaryNode = taskNode.SelectSingleNode("p:Summary", xnm);
        XmlNodeList predecessorNodes = taskNode.SelectNodes("p:PredecessorLink", xnm);
        row["UID"] = uidNode == null ? 0 : Convert.ToInt32(uidNode.InnerText);
        row["ID"] = idNode == null ? 0 : Convert.ToInt32(idNode.InnerText);
        row["WBS"] = wbsNode == null ? string.Empty : wbsNode.InnerText;
        row["OutlineLevel"] = outlineLevelNode == null ? 0 : Convert.ToInt32(outlineLevelNode.InnerText);
        row["OutlineNumber"] = outlineNumberNode == null ? string.Empty : outlineNumberNode.InnerText;
        bool isSummary = summaryNode == null ? false : (summaryNode.InnerText!=null && summaryNode.InnerText=="1")?true:false;
        row["IsSummary"] = isSummary;
        int oLevel = Convert.ToInt32(row["OutlineLevel"]);
        string namePrefix = "";
        for (int i = 0; i < oLevel; i++)
        {
          if (!isSummary)
          {
            namePrefix += "   ";
          }
          else
          {
            namePrefix += "  ";
          }
        }
        row["Name"] = nameNode==null?string.Empty:namePrefix + nameNode.InnerText;
        row["Select"] = false;

        foreach (XmlNode predecessorNode in predecessorNodes)
        {
            XmlNode predecessorUidNode = predecessorNode.SelectSingleNode("p:PredecessorUID", xnm);
            if (!row.IsNull(taskTable.Columns["Predecessors"]))
            {
                row["Predecessors"] += ";" + predecessorUidNode.InnerText;
            }
            else
            {
                row["Predecessors"] += predecessorUidNode.InnerText;
            }
        }

        taskTable.Rows.Add(row);
      }

      return taskTable;
    }

    public static DataTable GetProjectResources(string fileName)
    {
      XmlDocument xdoc = new XmlDocument();
      XmlElement root = xdoc.DocumentElement;
      xdoc.Load(fileName);
      XmlNamespaceManager xnm = new XmlNamespaceManager(xdoc.NameTable);
      xnm.AddNamespace("p", "http://schemas.microsoft.com/project");
      DataTable taskTable = new DataTable();
      taskTable.Columns.Add("Select", typeof(bool));
      taskTable.Columns.Add("ID", typeof(int));
      taskTable.Columns.Add("UID", typeof(int));
      taskTable.Columns.Add("Name", typeof(string));
      taskTable.Columns.Add("Type", typeof(string));

      foreach (XmlElement taskNode in xdoc.SelectNodes("//p:Resources/p:Resource", xnm))
      {
        DataRow row = taskTable.NewRow();
        XmlNode idNode = taskNode.SelectSingleNode("p:ID", xnm);
        XmlNode uidNode = taskNode.SelectSingleNode("p:UID", xnm);
        XmlNode nameNode = taskNode.SelectSingleNode("p:Name", xnm);
        XmlNode typeNode = taskNode.SelectSingleNode("p:Type", xnm);
        XmlNode isCostNode = taskNode.SelectSingleNode("p:IsCostResource", xnm);
        int rType = typeNode == null ? 0 : Convert.ToInt32(typeNode.InnerText);
        int isCostRes = isCostNode == null ? 0 : Convert.ToInt32(isCostNode.InnerText);
        string resType = "Work";
        if (rType == 0)
        {
          resType = isCostRes != 0 ? "Cost" : "Material";
        }
        row["ID"] = idNode == null ? 0 : Convert.ToInt32(idNode.InnerText);
        row["UID"] = uidNode == null ? 0 : Convert.ToInt32(uidNode.InnerText);
        row["Type"] = resType;
        row["Name"] = nameNode == null ? string.Empty : nameNode.InnerText;
        row["Select"] = false;
        taskTable.Rows.Add(row);
      }

      return taskTable;
    }
  }
}
